<template>
  <!-- 放置一个图标 -->
  <div>
    <!-- svg-icon内置全局组件：
    用法：icon-class="svg目录下图片名字"
     -->
    <svg-icon :icon-class="isFull? 'exit-fullscreen': 'fullscreen'" class="fullscreen" @click="toggleScreen" />
  </div>
</template>

<script>
import screenfull from 'screenfull'
export default {
  name: 'ScreenFull',
  data() {
    return {
      // 是否是全屏
      isFull: false
    }
  },
  //   事件监听
  created() {
    // 如果浏览器支持
    if (screenfull.isEnabled) {
      // 监听
      screenfull.on('change', () => {
        // 如果是全屏
        if (screenfull.isFullscreen) {
          // 变量为true
          this.isFull = true
        } else {
          // 否则为false
          this.isFull = false
        }
      })
    }
  },
  //   事件销毁
  beforeDestroy() {
    // 解绑事件监听
    screenfull.off('change')
  },
  methods: {
    toggleScreen() {
      // 判断是否可用
      if (!screenfull.isEnabled) {
        // 此时全屏不可用
        this.$message.warning('此时全屏组件不可用')
        return
      }
      //   如果可用 就可以全屏
      screenfull.toggle()
    }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
